package com.kurtphpr.sistema.teste;

import com.kurtphpr.sistema.cliente.Cliente;
import com.kurtphpr.sistema.cliente.ClienteRN;

import com.kurtphpr.sistema.util.HibernateUtil;
import java.util.Date;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.After;
import org.junit.AfterClass;
import static org.junit.Assert.assertEquals;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

public class ClienteTestMVC {

    private static Session sessao;
    private static Transaction transacao;

    @BeforeClass
    public static void abreConexao() {
        sessao = HibernateUtil.getSession().getCurrentSession();
        transacao = sessao.beginTransaction();
    }

    @AfterClass
    public static void fechaConexao() {
        try {
            transacao.commit();
        } catch (Throwable e) {
            System.out.println("deu problema no commit : " + e.getMessage());
        } finally {
            try {
                if (sessao.isOpen()) {
                    sessao.close();
                }
            } catch (Exception e2) {
                System.out.println("Deu erro no fechamento da sess�o!"
                        + e2.getMessage());
            }

        }

    }

    @Before
    public void setup() {
        Cliente c1 = new Cliente("76596743152", "cliente1@email.com",
                "Endereco teste 1", "Nome1", new Date(), 7.0f);
        Cliente c2 = new Cliente("21283431432", "cliente2@email.com",
                "Endereco teste 2", "Nome2", new Date(), 10.0f);
        Cliente c3 = new Cliente("17785397775", "cliente3@email.com",
                "Endereco teste 3", "Nome3", new Date(), 100.0f);
        Cliente c4 = new Cliente("90525139702", "cliente4@email.com",
                "Endereco teste 4", "Nome4", new Date(), 200.0f);
        Cliente c5 = new Cliente("77368376431", "cliente5@email.com",
                "Endereco teste 5", "Nome5", new Date(), 500.0f);

        ClienteRN clienteRN = new ClienteRN();

        clienteRN.salvar(c1);
        clienteRN.salvar(c2);
        clienteRN.salvar(c3);
        clienteRN.salvar(c4);
        clienteRN.salvar(c5);

    }

    @After
    public void limpaBanco() {

        ClienteRN clienteRN = new ClienteRN();
        List<Cliente> lista = clienteRN.listar();

        for (Cliente cliente : lista) {
            clienteRN.excluir(cliente);
        }
    }

    @Test
    public void salvarTest() {

        Cliente c1 = new Cliente();

        c1.setNome("Ronaldo");
        c1.setEndereco("Rua Teste");
        c1.setRenda(5000f);
        c1.setCpf("123456789");

        ClienteRN clienteRN = new ClienteRN();

        clienteRN.salvar(c1);

        assertEquals(true, true);
    }

    @Test
    public void listarTest() {

        ClienteRN clienteRN = new ClienteRN();
        List<Cliente> lista = clienteRN.listar();
        assertEquals(5, lista.size());
    }

    @Test
    public void excluirTest() {
        ClienteRN clienteRN = new ClienteRN();

        List<Cliente> lista = clienteRN.listar();

        Cliente clienteExcluido = lista.get(0);

        clienteRN.excluir(clienteExcluido);

        lista = clienteRN.listar();

        assertEquals(4, lista.size());

    }

    @Test
    public void pesquisarTest() {
        ClienteRN clienteRN = new ClienteRN();
        Cliente clientePesquisado = clienteRN.pesquisar("me2");

        assertEquals("cliente2@email.com", clientePesquisado.getEmail());
    }

    @Test
    public void alterarTest() {

        ClienteRN clienteRN = new ClienteRN();
        Cliente clientePesquisado = clienteRN.pesquisar("me2");

        assertEquals("cliente2@email.com", clientePesquisado.getEmail());

        clientePesquisado.setEndereco("Novo Endere�o");

        clienteRN.alterar(clientePesquisado);

        Cliente clienteAlterado = clienteRN.pesquisar("me2");

        assertEquals("Novo Endere�o", clienteAlterado.getEndereco());
    }
}
